package com.eeepay.eeepay_v2.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.ProfitInfo;
import com.eeepay.eeepay_v2.util.BaseGrpcTask;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.LeftRightText;
import com.eeepay.v2_library.view.TitleBar;

import java.util.regex.Pattern;

import cn.eeepay.api.grpc.nano.ProductApiServiceGrpc;
import cn.eeepay.api.grpc.nano.ProductSerProto;
import cn.eeepay.api.grpc.nano.ResMsgProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/29 0029.
 * 分润设置
 */
public class ProfitSetActivity extends ABBaseActivity{

    private TitleBar titleBar;
    private LeftRightText serviceName, cardType, tradeTime;
    private LabelEditText et_income;//每笔收益
    private TextView tv_explain;
    private TextView tv_tag;//显示的%或者元

    private ProfitInfo profitInfo;
    private String inCome;//

    @Override
    protected int getLayoutId() {
        return R.layout.activity_profit_set;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setShowRight(View.VISIBLE);
        titleBar.setRightTextView("保存");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        serviceName = getViewById(R.id.lrt_service_name);
        cardType = getViewById(R.id.lrt_card_type);
        tradeTime = getViewById(R.id.lrt_trade_time);
        et_income = getViewById(R.id.et_income);
        tv_explain = getViewById(R.id.tv_profit_explain);
        tv_tag = getViewById(R.id.tv_tag);

        if (bundle != null) {
            profitInfo = (ProfitInfo) bundle.getSerializable(Constant.PROFIT_INFO);
            serviceName.setRightText(profitInfo.getServiceName());
            cardType.setRightText("0".equals(profitInfo.getCardType()) ? "不限" :
                    "1".equals(profitInfo.getCardType()) ? "信用卡" : "储蓄卡");
            String tTime;
            if ("0".equals(profitInfo.getTradeTime())) {
                tTime = "不限";
            } else if ("1".equals(profitInfo.getTradeTime())) {
                tTime = "工作日";
            } else if ("2".equals(profitInfo.getTradeTime())) {
                tTime = "节假日";
            } else {
                tTime = "";
            }
            tradeTime.setRightText(tTime);
            et_income.getEditText().setFocusable(true);
            if (!TextUtils.isEmpty(profitInfo.getInCome())) {
                et_income.getEditText().setText(profitInfo.getInCome());
                et_income.getEditText().setSelection(profitInfo.getInCome().length());
            }
            else
            {
                et_income.getEditText().setText("");
                et_income.getEditText().setSelection(0);
            }

            //新增的内容
            //serviceType = 10000 or serviceType=10001 符号元
            String serviceType = profitInfo.getServiceType();
            if (serviceType.equals("10000") || serviceType.equals("10001") )
                tv_tag.setText("元");//符号% 或者元
            else
                tv_tag.setText("%");//符号% 或者元
        }

//        tv_explain.setText(getResources().getString(R.string.service_rate_explain));
    }


    @Override
    protected void eventOnClick() {
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                inCome = et_income.getEditContent().trim();
                if (!Pattern.matches(Constant.PROFIT_SET_REG, inCome)) {
                    showToast("分润设置不符合规则");
                } else {
//                    String[] strs = inCome.split("~");
//                    if (strs.length == 3) {
//                        if (Float.parseFloat(strs[0]) > Float.parseFloat(strs[2])) {
//                            showToast("保底金额不能大于封顶金额");
//                            return;
//                        }
//                    }
                    sendHttpRequest(0);
                }
            }
        });
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        super.sendHttpRequest(taskId);
        new GrpcTask().execute(Constant.URL.URL, Constant.URL.PORT);
    }

    private class GrpcTask extends BaseGrpcTask<ResMsgProto.ResMsg> {

        @Override
        protected ResMsgProto.ResMsg doInBack(ManagedChannel channel) {
            ProductApiServiceGrpc.ProductApiServiceBlockingStub stub = ProductApiServiceGrpc.newBlockingStub(channel);
            ProductSerProto.AgentShareApi request = new ProductSerProto.AgentShareApi();
            request.id = Integer.parseInt(profitInfo.getId());
            request.agentNo = profitInfo.getAgentId();
            LogUtils.d("TAG", " agent_id = " + profitInfo.getAgentId());
            request.serviceId = profitInfo.getServiceId();
            request.cardType = profitInfo.getCardType();
            request.holidaysMark = profitInfo.getTradeTime();
            request.shareSetting = inCome;
            ResMsgProto.ResMsg response = stub.saveAgentShare(request);
            return response;
        }

        @Override
        protected void updateUI(ResMsgProto.ResMsg result) {
            if (result == null) {
                showToast("设置失败");
                return;
            }
            if (!result.status) {
                showToast(result.msg);
            } else {
                showToast("设置成功");
            }
            finish();
        }

    }
}
